<?php 
## include required files
/*******************************/
require_once '../model/users.php';
require_once '../model/admindetails.php';
require_once '../model/budget.php';
require_once '../model/trade.php';
require_once '../model/state.php';
/*******************************/

## Create Objects
/*******************************/
$userObj = new Model_Users();
$adminObj = new Model_AdminDetails();
$budgetObj = new Model_Budget();
$objTrade = new Model_TRADE();
$stateObj = new Model_State();
/*******************************/
## Fetch admin user details by id
if($_GET['id'] != '' && $_GET['action'] == 'edit') {
	$job_id = base64_decode($_GET['id']);	
	$job_details = $userObj->getJobDetailsById($job_id);
	/*$getjobtype = $objTrade->getTradeCategoryDetailsById($job_details['jobtype_id']);	
	$job_details['jobtype_name'] = $getjobtype['trade_category'];*/
	
	$smarty->assign('job_details', $job_details);
}


## Fetch all active states
$stateArray = $stateObj->getAllStateWithActiveStatus();
//echo "<pre>"; print_r($stateArray); exit;
$smarty->assign('stateArray', $stateArray);

$getallradiusarea = $userObj->getAllLongLatByUserID(base64_decode($_GET['userid']));
$smarty->assign('latlongArray',$latlongArray);

$tradejobtypeArray = $objTrade->getAllJobTypeWithActiveStatus();
foreach($tradejobtypeArray as $tradejobtypeArray)
{	
	$newarray[] =	$tradejobtypeArray['parent_id'];
}
	
$tradecategoryArray = array_values(array_unique($newarray));
if($tradecategoryArray[0]!='')
{
		$imopldeids = @implode(",",$tradecategoryArray);
		$newtradecatArray = $objTrade->getAllTradesCategoryWithmultipleId($imopldeids);
}
//echo "<pre>";print_R($tradecatArray);die();
$smarty->assign("newtradecategoryArray", $newtradecatArray);

$jobtypeArray = $objTrade->getAllJobtypeByTradeId($job_details['trade_id']);
$smarty->assign("jobtypeArray", $jobtypeArray);

$getallactivebudgetArray = $budgetObj->getAllBudgetWithActiveStatus();
$smarty->assign("getallactivebudgetArray", $getallactivebudgetArray);


/*******************************/
if(isset($_POST['addpostjob_btn']) || trim($_POST['trade']) != '' && trim($_POST['job_desc']) != '' && trim($_POST['work_to_start']) != '') 
{
	extract($_POST);
	//echo"<pre>";print_R($_POST);die();
	## apply PHP validation for required filed
	if(trim($_POST['trade']) != '' && trim($_POST['job_desc']) != '' && trim($_POST['work_to_start']) != '') 
	{
			$job_id = $_POST['hidden_jobid'];
			$userPostArray = array();
			$userPostArray["trade_id"] 			= $_POST['trade'];
			if($_POST['jobtype']!='')
				$userPostArray["jobtype_id"]		= $_POST['jobtype'];					
			$userPostArray["job_desc"] 			= $_POST['job_desc'];
			$userPostArray['workstart'] 		= $_POST['work_to_start'];	
			$userPostArray['approx_budget']		= $_POST['approx_budget'];						
			$userPostArray['area'] 				= $_POST['postcode'];			
			$userPostArray['state_id'] 				= $_POST['state_id'];			
			
			$myArray = getLatLong($_POST['postcode']);
			$userPostArray['Latitude']			= $myArray['Latitude'];
			$userPostArray['Longitude']			= $myArray['Longitude'];
			//$userObj->editJobPostValueByJOBId($userPostArray, $job_id);
			
			$err = false;
			if($_FILES['joblogo']['name'] != '')
			{
				$size =	getimagesize($_FILES['joblogo']['tmp_name']);
				if($size[0] >= JOBIMAGEWIDTH && $size[1] >= JOBIMAGEHEIGHT) 
				{   
					@unlink('../uploads/job/'.$job_details['image']);
					$path = '../uploads/job/'; 
					$imageName    = $_FILES['joblogo']['name']; 
	
					$arrImageName = explode(".",$imageName);
					$position     = count($arrImageName);
					
					$fileName     = date('Ymdhis').".".$arrImageName[$position-1]; 
					@copy($_FILES['joblogo']['tmp_name'], $path.$fileName);
					
					$userPostArray['image']	= $fileName;
	
				}
				else
				{
				   $err = true;				   
				}
			}
			if($err == false)
			{
				$userObj->editJobPostValueByJOBId($userPostArray, $job_id);
				$_SESSION['msg'] = "<div class='success_msg'><span>Job updated successfully</span></div>";
				header('location:'.SITE_URL.'/admin/home.php?q=postjobs&userid='.$_GET['userid'].'&page='.$_GET['page']);
				exit;
			}
			else
			{
					$_SESSION['msg'] = "<div class='error_msg'><span>".JOBIMAGE_WIDTHHEIGHT."</span></div>";
			}			
			
			
	}
	else
	{ // php validation else
		$_SESSION['msg'] = '<div class="error_msg"><span>Please fill all required fields</span></div>';
	}
	// main if closed
}



## Assign success or error msg to smarty variable and unset session variable
if(trim($_SESSION['msg']) != '') {
	$smarty->assign('msg', $_SESSION['msg']);
	unset($_SESSION['msg']);
}

## Set active class variable for left menu
$smarty->assign('activeclass', 'postjobs');
$smarty->assign('mainmenu', '3');
## Unset all the objects created which are on this page
unset($userObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'postjobs/editpostjob.tpl');
unset($smarty);
?>